package com.mdd.admin.controller.store;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IStoreService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validate.store.StoreCreateValidate;
import com.mdd.common.validate.store.StoreSearchValidate;
import com.mdd.common.validate.store.StoreStatusValidate;
import com.mdd.common.validate.store.StoreUpdateValidate;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.common.vo.store.StoreDetailVo;
import com.mdd.common.vo.store.StoreListedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/store")
@Api(tags = "门店管理")
public class StoreController {

    @Resource
    IStoreService iStoreService;

    @GetMapping("/list")
    @ApiOperation(value = "门店列表")
    public AjaxResult<PageResult<StoreListedVo>> list(@Validated PageValidate pageValidate, @Validated StoreSearchValidate searchValidate) {
        PageResult<StoreListedVo> list = iStoreService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "门店详情")
    public AjaxResult<StoreDetailVo> detail(@Validated @IDMust() @RequestParam("storeId") Integer id) {
        StoreDetailVo detail = iStoreService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "门店新增")
    @PostMapping("/add")
    @ApiOperation(value = "门店新增")
    public AjaxResult<Object> add(@Validated @RequestBody StoreCreateValidate createValidate) {
        iStoreService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "门店编辑")
    @PostMapping("/edit")
    @ApiOperation(value = "门店编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody StoreUpdateValidate updateValidate) {
        iStoreService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "门店删除")
    @PostMapping("/del")
    @ApiOperation(value = "门店删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iStoreService.del(idValidate.getId());
        return AjaxResult.success();
    }

    @Log(title = "门店状态修改")
    @PostMapping("/status")
    @ApiOperation(value = "门店状态修改")
    public AjaxResult<Object> status(@Validated @RequestBody StoreStatusValidate storeStatusValidate) {
        iStoreService.status(storeStatusValidate);
        return AjaxResult.success();
    }

}
